package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.service.wx.WxGoodsService;
import com.cskaoyan.market.util.VoUtil;
import com.cskaoyan.market.vo.wx.CategoryVo;
import com.cskaoyan.market.vo.wx.HotGoodsVo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;


/**
 * @ClassName: GoodsServiceImpl
 * @Auther: lyz
 * @Date: 2023/05/20/14:09
 * @Description:
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketGrouponMapper marketGrouponMapper;
    @Autowired
    MarketIssueMapper marketIssueMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    private static final Integer LIMIT = 10;

    @Override
    public Integer count() {
        Integer count = 0;
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.or();
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> list = marketGoodsMapper.selectByExample(marketGoodsExample);
        for (MarketGoods goods : list) {
            count++;
        }
        return count;
    }

//    @Override
//    public List<MarketGoods> goodList(Integer categoryId, Integer page, Integer limit) {
//        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
//        MarketGoodsExample.Criteria or = marketGoodsExample.or().andCategoryIdEqualTo(categoryId);
//        PageHelper.startPage(page, limit);
//        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
//        return marketGoods;
//    }
//
//    @Override
//    public List<MarketCategory> categoryList(Integer categoryId, Integer page, Integer limit) {
//        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(categoryId);
//        String level = category.getLevel();
//        MarketCategoryExample categoryExample = new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria = categoryExample.or().andLevelEqualTo(level);
//        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(categoryExample);
//        // 共同点是level l2
//        if (categoryList.size() > LIMIT) {
//            categoryList = categoryList.subList(0, LIMIT);
//        }
//        return categoryList;
//    }


//    @Override
//    public Map<String, Object> goodList(Integer categoryId, Integer page, Integer limit) {
//        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
//        marketGoodsExample.or().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId);
//        PageHelper.startPage(page, limit);
//        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
//        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(categoryId);
//        String level = category.getLevel();
//        MarketCategoryExample categoryExample = new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria = categoryExample.or().andLevelEqualTo(level);
//        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(categoryExample);
//        // 共同点是level l2
//        if (categoryList.size() > LIMIT) {
//            categoryList = categoryList.subList(0, LIMIT);
//        }
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("filterCategoryList", VoUtil.objectToVolist(categoryList, CategoryVo.class));
//        map.put("limit", limit);
//        map.put("page", page);
//        map.put("pages", (marketGoods.size() / limit) + 1);
//        map.put("total", marketGoods.size());
//        if (marketGoods.size() > LIMIT) {
//            marketGoods = marketGoods.subList(0, LIMIT);
//        }
//        map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
//        return map;
//    }


    @Override
    public Map<String, Object> goodList(String keyword, Integer categoryId, Integer page, Integer limit, String sort, String order, Integer userId, Integer brandId, boolean isNew, boolean isHot) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        if (brandId != null) {
            marketGoodsExample.or().andBrandIdEqualTo(brandId);
            List<MarketGoods> marketGoodsBrandsList = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<Integer> marketGoodsId = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            for (MarketGoods marketGoods : marketGoodsBrandsList) {
                Integer marketGoodsCategoryId = marketGoods.getCategoryId();
                marketGoodsId.add(marketGoodsCategoryId);
                MarketCategoryExample example = new MarketCategoryExample().or().andIdIn(marketGoodsId).example();
                List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
                map.put("filterCategoryList", VoUtil.objectToVolist(marketCategories, CategoryVo.class));
                map.put("limit", limit);
                map.put("page", page);
                map.put("pages", (marketGoodsBrandsList.size() / limit) + 1);
                map.put("total", marketGoodsBrandsList.size());
                if (marketGoodsBrandsList.size() > limit) {
                    marketGoodsBrandsList = marketGoodsBrandsList.subList(0, limit);
                }
                map.put("list", VoUtil.objectToVolist(marketGoodsBrandsList, HotGoodsVo.class));
            }
            return map;
        }
        if (!StringUtils.isEmpty(keyword)) {
            marketGoodsExample.or().andNameLike("%" + keyword + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            marketGoodsExample.setOrderByClause(sort + " " + order);
        }
        if (!StringUtils.isEmpty(keyword) && categoryId == 0) {
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<Object> list = new ArrayList<>();
            for (MarketGoods marketGood : marketGoods) {
                Integer categoryId1 = marketGood.getCategoryId();
                MarketCategory category = marketCategoryMapper.selectByPrimaryKey(categoryId1);
                list.add(category);
                HashSet hashSet = new HashSet(list);
                list.clear();
                list.addAll(hashSet);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("filterCategoryList", VoUtil.objectToVolist(list, CategoryVo.class));
            map.put("limit", limit);
            map.put("page", page);
            map.put("pages", (marketGoods.size() / limit) + 1);
            map.put("total", marketGoods.size());
            if (marketGoods.size() > limit) {
                marketGoods = marketGoods.subList(0, limit);
            }
            map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
            if (userId != null) {
                MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
                historyExample.or().andDeletedEqualTo(false).andUserIdEqualTo(userId).andKeywordEqualTo(keyword);
                List<MarketSearchHistory> histories = searchHistoryMapper.selectByExample(historyExample);
                if (histories.size() == 0) {
                    MarketSearchHistory history = new MarketSearchHistory();
                    history.setUserId(userId);
                    history.setKeyword(keyword);
                    history.setAddTime(LocalDateTime.now());
                    history.setUpdateTime(LocalDateTime.now());
                    history.setDeleted(false);
                    searchHistoryMapper.insertSelective(history);
                }
            }
            return map;
        }
        if ((!StringUtils.isEmpty(keyword)) && categoryId != 0) {
            Map<String, Object> map = new HashMap<>();
            marketGoodsExample.or().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId).andNameLike("%" + keyword + "%");
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<MarketCategory> marketCategoryList = new ArrayList<>();
            List<MarketGoods> goodsList = new ArrayList<>();
            for (MarketGoods marketGood : marketGoods) {
                Integer categoryId1 = marketGood.getCategoryId();
                MarketCategory category1 = marketCategoryMapper.selectByPrimaryKey(categoryId1);
                marketCategoryList.add(category1);
                HashSet hashSet = new HashSet(marketCategoryList);
                marketCategoryList.clear();
                marketCategoryList.addAll(hashSet);
            }
            goodsList = marketGoodsMapper.selectByExample(new MarketGoodsExample().or().andDeletedEqualTo(false).andNameLike("%" + keyword + "%").andCategoryIdEqualTo(categoryId).example());
            map.put("filterCategoryList", VoUtil.objectToVolist(marketCategoryList, CategoryVo.class));
            map.put("limit", limit);
            map.put("page", page);
            map.put("pages", (marketGoods.size() / limit) + 1);
            map.put("total", marketGoods.size());
            map.put("list", VoUtil.objectToVolist(goodsList, HotGoodsVo.class));
            return map;
        }

        if (isNew == true && categoryId == 0) {
            marketGoodsExample.or().andIsNewEqualTo(true);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<Integer> marketGoodsId = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            for (MarketGoods marketGood : marketGoods) {
                Integer goodCategoryId = marketGood.getCategoryId();
                marketGoodsId.add(goodCategoryId);
                MarketCategoryExample example = new MarketCategoryExample().or().andIdIn(marketGoodsId).example();
                List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
                map.put("filterCategoryList", VoUtil.objectToVolist(marketCategories, CategoryVo.class));
                map.put("limit", limit);
                map.put("page", page);
                map.put("pages", (marketGoods.size() / limit) + 1);
                map.put("total", marketGoods.size());
                if (marketGoods.size() > limit) {
                    marketGoods = marketGoods.subList(0, limit);
                }
                map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
            }
            return map;
        }
        //
        if (isNew == true && categoryId != 0) {
            Map<String, Object> map = new HashMap<>();
            marketGoodsExample.or().andIsNewEqualTo(true);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<MarketCategory> caIds = new ArrayList<>();
            for (MarketGoods marketGood : marketGoods) {
                Integer categoryId1 = marketGood.getCategoryId();
                MarketCategory category1 = marketCategoryMapper.selectByPrimaryKey(categoryId1);
                caIds.add(category1);
                HashSet hashSet = new HashSet(caIds);
                caIds.clear();
                caIds.addAll(hashSet);
            }
            marketGoods = marketGoodsMapper.selectByExample(new MarketGoodsExample().or().andDeletedEqualTo(false).andIsNewEqualTo(true).andCategoryIdEqualTo(categoryId).example());
            map.put("filterCategoryList", VoUtil.objectToVolist(caIds, CategoryVo.class));
            map.put("limit", limit);
            map.put("page", page);
            map.put("pages", (marketGoods.size() / limit) + 1);
            map.put("total", marketGoods.size());
            map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
            return map;
        }
        //
        if (isHot == true && categoryId == 0) {
            marketGoodsExample.or().andIsHotEqualTo(true);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<Integer> marketGoodsId = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            for (MarketGoods marketGood : marketGoods) {
                Integer goodCategoryId = marketGood.getCategoryId();
                marketGoodsId.add(goodCategoryId);
                MarketCategoryExample example = new MarketCategoryExample().or().andIdIn(marketGoodsId).example();
                List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
                map.put("filterCategoryList", VoUtil.objectToVolist(marketCategories, CategoryVo.class));
                map.put("limit", limit);
                map.put("page", page);
                map.put("pages", (marketGoods.size() / limit) + 1);
                map.put("total", marketGoods.size());
                if (marketGoods.size() > limit) {
                    marketGoods = marketGoods.subList(0, limit);
                }
                map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
            }
            return map;
        }
        if (isHot == true && categoryId != 0) {
            Map<String, Object> map = new HashMap<>();
            marketGoodsExample.or().andIsHotEqualTo(true);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            List<MarketCategory> caIds = new ArrayList<>();
            for (MarketGoods marketGood : marketGoods) {
                Integer categoryId1 = marketGood.getCategoryId();
                MarketCategory category1 = marketCategoryMapper.selectByPrimaryKey(categoryId1);
                caIds.add(category1);
                HashSet hashSet = new HashSet(caIds);
                caIds.clear();
                caIds.addAll(hashSet);
            }
            marketGoods = marketGoodsMapper.selectByExample(new MarketGoodsExample().or().andDeletedEqualTo(false).andIsHotEqualTo(true).andCategoryIdEqualTo(categoryId).example());
            map.put("filterCategoryList", VoUtil.objectToVolist(caIds, CategoryVo.class));
            map.put("limit", limit);
            map.put("page", page);
            map.put("pages", (marketGoods.size() / limit) + 1);
            map.put("total", marketGoods.size());
            map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
            return map;
        }


        marketGoodsExample.or().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId);
        PageHelper.startPage(page, limit);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(categoryId);
        String level = category.getLevel();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.or().andLevelEqualTo(level);
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(categoryExample);
        // 共同点是level l2
        if (categoryList.size() > limit) {
            categoryList = categoryList.subList(0, limit);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("filterCategoryList", VoUtil.objectToVolist(categoryList, CategoryVo.class));
        map.put("limit", limit);
        map.put("page", page);
        map.put("pages", (marketGoods.size() / limit) + 1);
        map.put("total", marketGoods.size());
        if (marketGoods.size() > limit) {
            marketGoods = marketGoods.subList(0, limit);
        }
        map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));

        return map;
    }

    @Override
    public List<MarketCategory> brotherCategory(Integer id) {
        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(id);
        String level = category.getLevel();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        if (level.equals("L1")) {
            categoryExample.or().andDeletedEqualTo(false).andPidEqualTo(id);
        } else if (level.equals("L2")) {
            categoryExample.or().andDeletedEqualTo(false).andPidEqualTo(category.getPid());
        }
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(categoryExample);
        return categoryList;
    }

    @Override
    public MarketCategory currentCategory(Integer id) {
        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(id);
        String level = category.getLevel();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        if (level.equals("L1")) {
            categoryExample.or().andDeletedEqualTo(false).andPidEqualTo(id);
        } else if (level.equals("L2")) {
            categoryExample.or().andDeletedEqualTo(false).andIdEqualTo(id);
        }
        MarketCategory category1 = marketCategoryMapper.selectOneByExample(categoryExample);
        return category1;
    }

    @Override
    public MarketCategory parentCategory(Integer id) {
        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(id);
        String level = category.getLevel();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        if (level.equals("L1")) {
            categoryExample.or().andDeletedEqualTo(false).andIdEqualTo(id);
        } else if (level.equals("L2")) {
            categoryExample.or().andDeletedEqualTo(false).andIdEqualTo(category.getPid());
        }
        MarketCategory category1 = marketCategoryMapper.selectOneByExample(categoryExample);
        return category1;
    }
}
